/**************************************************************************
 * CLASS:		MiscImageDragView
 * Copyright (C) 1995 Robert Todd Thomas
 * Use is governed by the MiscKit license
 *
 *	See the header file for more information on this class.
 **************************************************************************/

#import <appkit/Pasteboard.h> 
#import "MiscImageDragView.h"

#define MISC_IMAGEDRAGVIEW_VERSION 1


@implementation MiscImageDragView

+ initialize
{
	// Set the versioning for archiving.
	if (self == [MiscImageDragView class])
		[self setVersion: MISC_IMAGEDRAGVIEW_VERSION];
	
	return self;
}



// Register to accept dragging, and setup some defaults.

- initFrame: (NXRect *)frameRect
{	
	[super initFrame: frameRect];
	[self setDragIconRepresentation: YES];

	return self;
}



- initDragTypes
{
  const char *const types[] = { NXPostScriptPboardType, NXTIFFPboardType, 	
  								NXFilenamePboardType };

	[self registerForDraggedTypes: (const char *const *)&types count: 3];
	return self;
}



//  Sets whether the TIFF itself or its TIFF icon representation is dragged.
 
- setDragIconRepresentation: (BOOL)aBool
{
	dragIconRepresentation = aBool;
	return self;
}



- (BOOL)dragIconRepresentation
{
	return dragIconRepresentation;
}



// Put the image data on the NXTIFFPboardType. Also set the image that is
// going to be used for the drag, depending upon whether 
// dragIconRepresentation is true.

- (BOOL)setupForSourceDrag
{
  id  dragPB = [self draggingPasteboard];
  NXStream  *imageStream = NXOpenMemory (NULL, 0, NX_WRITEONLY);
  const char *const types[] = { NXTIFFPboardType };
	
	// write the NXImage in TIFF form (it's original size)
	
	[dragPB declareTypes: types num: 1 owner: self];
	
	// put the image back to regular size and write it
	
	[ [self image] setSize: &imageSize];
	[ [self image] writeTIFF: imageStream];
	[dragPB writeType: NXTIFFPboardType fromStream: imageStream];

	NXCloseMemory (imageStream, NX_FREEBUFFER);		

	// if use icon rep. grab the image from somewhere that everyone has
	
	if ([self dragIconRepresentation])
		[self setDragImage:  [ [NXImage alloc] initFromFile: 	
				"/NextApps/Preview.app/tiff.tiff"] freeWhenDone: YES];	
	else
		[self setDragImage: [self image] ];	

	return YES;
}



// Adjust the dragPoint so that you grab the image in the center.

- calculateDragPoint: (NXPoint *)dragPoint andOffset: (NXPoint *)offset
{
	if ([self dragIconRepresentation])
	{
		dragPoint->x -= 24.0;
		dragPoint->y -= 24.0;
	 }
	else
	{
	  NXSize  imSize;
	  
	  	[ [self image] getSize: &imSize];
		dragPoint->x -= imSize.width / 2.0;
		dragPoint->y -= imSize.height / 2.0;
	 }
	 
	return self;
}



// This sure is a complicated method. Actually NXImage takes care
// of everything. It checks for filenames, correct extensions, data
// on the correct pasteboards, and even accepts non native images if 
// you have the filters.
		
- (BOOL)prepareForDragOperation: sender
{
	if ([NXImage canInitFromPasteboard: [sender draggingPasteboard] ])
	    return YES;

	[self cleanupAfterDestinationDrag];
	[self display];
	
	return NO;
}



// Take the pasteboard data and create the image.

- (BOOL)performDragOperation: sender
{
	[self setImage: [ [NXImage alloc] initFromPasteboard: 
			[sender draggingPasteboard] ] ];

	return YES;
}



// Archiving methods

- read: (NXTypedStream *)stream
{
	int  version;
	
	[super read: stream];
	
	version = NXTypedStreamClassVersion(stream, "MiscImageDragView");
	switch (version)
	{
		case 0:
		break;
		
		case MISC_IMAGEDRAGVIEW_VERSION:
			NXReadType (stream, "c", &dragIconRepresentation);
		break;
	 }
	 	
	return self;
}



- write: (NXTypedStream *)stream
{
	[super write: stream];
	NXWriteType (stream, "c", &dragIconRepresentation);

	return self;
}



- (const char *)stringValue
{	// we don't currently remember the filename, so we don't
	// know where the image came from...
    return [super stringValue];
}



- setStringValue:(const char *)aValue
{
    return [self setImageByFilename:aValue];
}

@end


/*****************************************************************************
  CHANGES:
  	Version 0.4, November 30, 1994 (Todd)
		1) Just made it compatible with the changes made in MiscDragView and
			added archive versioning.

 *****************************************************************************/
 